package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.InfoCategoryInfo;
import com.ruoyi.system.service.IInfoCategoryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * 新闻栏目关系表Controller
 * 
 * @author ruoyi
 * @date 2022-12-02
 */
@Controller
@RequestMapping("/system/infocategoryinfo")
public class InfoCategoryInfoController extends BaseController
{
    private String prefix = "system/infocategoryinfo";

    @Autowired
    private IInfoCategoryInfoService infoCategoryInfoService;

    @PreAuthorize("@ss.hasPermi('system:infocategoryinfo:view')")
    @GetMapping()
    public String info()
    {
        return prefix + "/infocategoryinfo";
    }

    /**
     * 查询新闻栏目关系表列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InfoCategoryInfo infoCategoryInfo)
    {
        startPage();
        List<InfoCategoryInfo> list = infoCategoryInfoService.selectInfoCategoryInfoList(infoCategoryInfo);
        return getDataTable(list);
    }

    /**
     * 导出新闻栏目关系表列表
     */
    /*@RequiresPermissions("system:infocategoryinfo:export")
    @Log(title = "新闻栏目关系表", businessType = BusinessType.EXPORT)*/
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InfoCategoryInfo infoCategoryInfo)
    {
        List<InfoCategoryInfo> list = infoCategoryInfoService.selectInfoCategoryInfoList(infoCategoryInfo);
        ExcelUtil<InfoCategoryInfo> util = new ExcelUtil<InfoCategoryInfo>(InfoCategoryInfo.class);
        return util.exportExcel(list, "新闻栏目关系表数据");
    }

    /**
     * 新增新闻栏目关系表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存新闻栏目关系表
     */
    /*@RequiresPermissions("system:infocategoryinfo:add")
    @Log(title = "新闻栏目关系表", businessType = BusinessType.INSERT)*/
    @Log(title = "转载新闻", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("infoid") String infoid ,@RequestParam("categoryid") String categoryid,@RequestParam("shifouzhiding") int shifouzhiding,@RequestParam("ordernum") int ordernum)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String mmDeptId = user.getDeptId().toString();

        InfoCategoryInfo infoCategoryInfo = new InfoCategoryInfo();
        infoCategoryInfo.setInfoid(infoid);
        infoCategoryInfo.setCategoryid(categoryid);

        List<InfoCategoryInfo> infoCategoryInfoList = infoCategoryInfoService.selectInfoCategoryInfoList(infoCategoryInfo);
        if (infoCategoryInfoList.size()>0){
            return error("该栏目已有此新闻，请勿重复发布");
        }

        infoCategoryInfo.setId(UUID.randomUUID().toString());
        infoCategoryInfo.setShifouzhiding(new Long(shifouzhiding));
        infoCategoryInfo.setText1(mmDeptId);
        infoCategoryInfo.setOrdernum(new Long(ordernum));
        return toAjax(infoCategoryInfoService.insertInfoCategoryInfo(infoCategoryInfo));
    }

    /**
     * 修改新闻栏目关系表
     */
    /*@RequiresPermissions("system:infocategoryinfo:edit")*/
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        InfoCategoryInfo infoCategoryInfo = infoCategoryInfoService.selectInfoCategoryInfoById(id);
        mmap.put("infoCategoryInfo", infoCategoryInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存新闻栏目关系表
     */
    /*@RequiresPermissions("system:infocategoryinfo:edit")
    @Log(title = "新闻栏目关系表", businessType = BusinessType.UPDATE)*/
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated @RequestBody InfoCategoryInfo infoCategoryInfo)
    {
        return toAjax(infoCategoryInfoService.updateInfoCategoryInfo(infoCategoryInfo));
    }

    /**
     * 删除新闻栏目关系表
     */
   /* @RequiresPermissions("system:infocategoryinfo:remove")
    @Log(title = "新闻栏目关系表", businessType = BusinessType.DELETE)*/
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(infoCategoryInfoService.deleteInfoCategoryInfoByIds(ids));
    }
}
